﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Example07.aspx.cs" Inherits="Chapter30.Example07" %>

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>代码清单 30-7 捕捉事件</title>
    <meta name="author" content="Adam Freeman" />
    <meta name="description" content="A simple example" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
    <style title="cor styles">
        p {
            background: gray;
            color: white;
            padding: 10px;
            margin: 5px;
            border: thin solid black;
        }

        span {
            background: white;
            color: black;
            padding: 2px;
            cursor: default;
        }
    </style>
</head>
<body>
    <p id="block1">
        There are lots of different kinds of fruit - there are you over 500 varieties 
        of <span id="banana">banana</span> alone. By the time we add the countless 
        types of apples, oranges, and other well-known fruit, we are 
        faced with thousands of choices.
    </p>
    <script>
        var banana = document.getElementById("banana");
        var textblock = document.getElementById("block1");

        banana.addEventListener("mouseover", handleMouseEvent);
        banana.addEventListener("mouseout", handleMouseEvent);
        textblock.addEventListener("mouseover", handleDescendantEvent, true);
        textblock.addEventListener("mouseout", handleDescendantEvent, true);

        function handleMouseEvent(e) {
            if (e.type == 'mouseover') {
                e.target.style.background = 'white';
                e.target.style.color = 'black';
            } else {
                e.target.style.removeProperty('color');
                e.target.style.removeProperty('background');
            }
        }

        function handleDescendantEvent(e) {
            if (e.type == "mouseover" && e.eventPhase == Event.CAPTURING_PHASE) {
                e.target.style.border = "thick solid red";
                e.currentTarget.style.border = "thick double black";
            } else if (e.type == "mouseout" && e.eventPhase == Event.CAPTURING_PHASE) {
                e.target.style.removeProperty("border");
                e.currentTarget.style.removeProperty("border");
            }
        }
    </script>
</body>
</html>
